<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>往来单位基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>选择变更往来单位</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="mid" class="layui-input mid" onclick="openPage('../public/choose_dealcom.php?dom=mid&page=dealbian&isbian=1','80%','80%')">
                            <div class="choose_div" id="mid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?dom=mid&page=dealbian&isbian=1','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="colorhong" style="line-height: 53px;margin-left: 20px">提示：以下信息为变更后信息，本变更申请通过后，将覆盖原有信息！</div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>单位名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>所属分类</label>
                        <div class="layui-input-block">
                            <div id="type"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">单位性质</label>
                        <div class="layui-input-block">
                            <div id="nature"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">法定代表人</label>
                        <div class="layui-input-block">
                            <input type="text" name="legaler" id="legaler" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">联系地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="lianaddr" id="lianaddr" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">联系人</label>
                        <div class="layui-input-block">
                            <input type="text" name="lianer" id="lianer" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">联系方式</label>
                        <div class="layui-input-block">
                            <input type="text" name="liantel" id="liantel" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">主营业务</label>
                        <div class="layui-input-block">
                            <textarea id="zhuwu" name="zhuwu" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>开票信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税务类型</label>
                        <div class="layui-input-block">
                            <div id="taxtype"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开票类型</label>
                        <div class="layui-input-block">
                            <div id="piaotype"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">统一社会信用代码</label>
                        <div class="layui-input-block">
                            <input type="text" name="ratenum" id="ratenum" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税点</label>
                        <div class="layui-input-block">
                            <input type="text" name="rate" id="rate" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="unitaddr" id="unitaddr" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="unittel" id="unittel" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开户行</label>
                        <div class="layui-input-block">
                            <input type="text" name="unitbkname" id="unitbkname" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="unitbknum" id="unitbknum" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>账户信息</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>是否默认账户</th>
                            <th><text>*</text>账户名</th>
                            <th>开户行及行号</th>
                            <th>银行账号</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    chu_flow_edit_yushen();

    $("#commentForm").validate({
        rules: {
            name: {required: true},
        },
        messages:{
            name: {required: "请输入"},
        }
    });

    var Arr_type, Arr_nature, Arr_taxtype, Arr_piaotype;
    function set_type(value=''){
        xmselect_dan('type', Arr_type, value, function (res) {
            if(res.arr && res.arr.length > 0){
                $("#type").find('xm-select').removeClass('error');
            }else{
                $("#type").find('xm-select').addClass('error');
            }
        });
    }
    function set_nature(value=''){
        xmselect_dan('nature', Arr_nature, value, function (res) {
            if(res.arr && res.arr.length > 0){
                $("#nature").find('xm-select').removeClass('error');
            }else{
                $("#nature").find('xm-select').addClass('error');
            }
        });
    }
    function set_taxtype(value=''){
        xmselect_dan('taxtype', Arr_taxtype, value);
    }
    function set_piaotype(value=''){
        xmselect_dan('piaotype', Arr_piaotype, value);
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();
        facade.ajax({type: 'get', url: ask_flowdata_edit_dealbian, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                chu_flow_qian(data);

                var midinfo = res.data.midinfo;
                if(midinfo){
                    set_choose_btn('mid',midinfo.id,midinfo.name);
                }


                $("#name").val(data.name);
                $("#legaler").val(data.legaler);
                $("#lianaddr").val(data.lianaddr);
                $("#lianer").val(data.lianer);
                $("#liantel").val(data.liantel);
                $("#zhuwu").val(data.zhuwu);
                $("#explain").val(data.explain);

                $("#ratenum").val(data.ratenum);
                $("#rate").val(data.rate);
                $("#unitaddr").val(data.unitaddr);
                $("#unittel").val(data.unittel);
                $("#unitbkname").val(data.unitbkname);
                $("#unitbknum").val(data.unitbknum);

                Arr_type = res.data.type;
                Arr_nature = res.data.nature;
                Arr_taxtype = res.data.taxtype;
                Arr_piaotype = res.data.piaotype;

                set_type(data.type);
                set_nature(data.nature);
                set_taxtype(data.taxtype);
                set_piaotype(data.piaotype);


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<select class="layui-select isdea" lay-filter="isdea">';
                listAddStr += '<option value="1">是</option>';
                listAddStr += '<option value="0" selected>否</option>';
                listAddStr += '</select>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input zhangname"></td>';
                listAddStr += '<td><input type="text" class="layui-input bankname"></td>';
                listAddStr += '<td><input type="text" class="layui-input banknum"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);



                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td>';
                        str += '<select class="layui-select isdea" lay-filter="isdea">';
                        if(list[key].isdea > 0){
                            str += '<option value="1" selected>是</option>';
                            str += '<option value="0">否</option>';
                        }else{
                            str += '<option value="1">是</option>';
                            str += '<option value="0" selected>否</option>';
                        }
                        str += '</select>';
                        str += '</td>';
                        str += '<td><input type="text" class="layui-input zhangname" value="'+list[key].zhangname+'"></td>';
                        str += '<td><input type="text" class="layui-input bankname" value="'+list[key].bankname+'"></td>';
                        str += '<td><input type="text" class="layui-input banknum" value="'+list[key].banknum+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }
                update_refresh_row_func();

                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('select(isdea)', function(data){
            if(data.value == 1){
                $(".isdea").val(0);
                var checkIndex = $($(this).parents('tr')).index();
                var checkDom = $(".isdea");
                $(checkDom[checkIndex]).val(1);
            }
            render_from();
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');

            data.field.mid = get_choose_val('mid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();

                formValid('mid');
                if(data.field.mid == undefined || !data.field.mid){
                    validRes = false;
                }

                if(!data.field.type){
                    $("#type").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#type").find('xm-select').removeClass('error');
                }

                if(!validRes){return false;}
            }



            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var isdea = $(this).find('.isdea').val();
                    var zhangname = $(this).find('.zhangname').val();
                    var bankname = $(this).find('.bankname').val();
                    var banknum = $(this).find('.banknum').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!zhangname){
                            throw new Error('第'+index+'行，请输入账户名');
                        }
                        if(!bankname){
                            throw new Error('第'+index+'行，请输入开户行及行号');
                        }
                        if(!banknum){
                            throw new Error('第'+index+'行，请输入银行账号');
                        }
                    }


                    list.push({
                        isdea:isdea,
                        zhangname:zhangname,
                        bankname:bankname,
                        banknum:banknum,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');

            public_flow_submit(data.field, ask_flowdata_add_dealbian, ask_flowdata_edit_dealbian, $(this).attr('but'));
            return false;
        });

    });


    function update_refresh_row_func() {
        render_date();
        render_from();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }


</script>
</body>
</html>
